<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-select
          v-model="query.garbageTypeValue"
          placeholder="垃圾类型"
          class="handle-select mr10"
          @change="selectGarbageValue"
          clearable
        >
          <el-option
            v-for="item in garbageTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

        <el-input
          v-model="query.no"
          placeholder="流水号"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-input
          v-model="query.carCode"
          placeholder="车号"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>

        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="毛重开始日期"
            end-placeholder="毛重结束日期"
          >
          </el-date-picker>
        </div>

        <el-button type="primary" @click="handleSearch">查询</el-button>

        <el-button
          type="primary"
          v-if="wdmPermissions.includes('sorted:weight:export')"
          @click="handleExport"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="no"
          align="center"
          label="流水号"
        ></el-table-column>
        <el-table-column
          prop="carCode"
          align="center"
          label="车号"
        ></el-table-column>
        <el-table-column
          prop="garbageTypeName"
          align="center"
          label="垃圾类型"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="recycleTypeName"
          label="可回收品类"
        ></el-table-column>
        <el-table-column
          prop="grossWeight"
          align="center"
          label="毛重(kg)"
        ></el-table-column>
        <el-table-column
          prop="tareWeight"
          align="center"
          label="皮重(kg)"
        ></el-table-column>
        <el-table-column
          prop="netWeight"
          align="center"
          label="净重(kg)"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="grossDateTime"
          label="毛重时间"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="wdmPermissions.includes('sorted:weight:update')"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="wdmPermissions.includes('sorted:weight:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form
        ref="editFor"
        :model="editForm"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item label="流水号" prop="no">
          <el-input v-model="editForm.no"></el-input>
        </el-form-item>

        <el-form-item label="车号" prop="carCode">
          <el-input v-model="editForm.carCode"></el-input>
        </el-form-item>

        <el-form-item label="毛重" prop="grossWeight">
          <el-input v-model="editForm.grossWeight"></el-input>
        </el-form-item>

        <el-form-item label="皮重" prop="tareWeight">
          <el-input v-model="editForm.tareWeight"></el-input>
        </el-form-item>

        <el-form-item label="净重" prop="netWeight">
          <el-input v-model="editForm.netWeight"></el-input>
        </el-form-item>

        <el-form-item label="毛重时间" prop="grossDateTime">
          <el-input v-model="editForm.grossDateTime" disabled></el-input>
        </el-form-item>

        <el-form-item label="垃圾类型" prop="garbageTypeValue">
          <el-select
            v-model="garbageTypeValue"
            placeholder="请选择"
            @change="selectGarbageValue"
            clearable
          >
            <el-option
              v-for="item in garbageTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="可回收类型">
          <el-select
            v-model="recycleTypeValue"
            placeholder="请选择"
            @change="selectRecycleValue"
          >
            <el-option
              v-for="item in recycleTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from "utils/util";
import NProgress from "nprogress";
import { mapGetters } from "vuex";
export default {
  name: "weighbridgedatamanage",
  data() {
    return {
      wdmPermissions: [],
      rules: {
        no: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        carCode: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        grossWeight: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        tareWeight: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        netWeight: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        grossDateTime: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        garbageTypeValue: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
      },
      recycleTypeValue: "",
      recycleTypeList: [
        {
          value: "21",
          label: "金属",
        },
        {
          value: "22",
          label: "塑料",
        },
        {
          value: "23",
          label: "玻璃",
        },
        {
          value: "24",
          label: "纺织",
        },
        {
          value: "25",
          label: "纸类",
        },
        {
          value: "26",
          label: "其他",
        },
        {
          value: "27",
          label: "小家电",
        },
      ],
      garbageTypeValue: "",
      garbageTypeList: [
        {
          value: "1",
          label: "易腐垃圾",
        },
        {
          value: "2",
          label: "可回收物",
        },
        {
          value: "3",
          label: "其他垃圾",
        },
        {
          value: "4",
          label: "有害垃圾",
        },
        {
          value: "5",
          label: "大件垃圾",
        },
        {
          value: "6",
          label: "装修垃圾",
        },
      ],
      query: {
        garbageTypeValue: "",
        no: "",
        carCode: "",
        // address: "",
        // name: "",
        pageIndex: 1,
        pageSize: 20,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      // form: {},
      value2: "",
      editForm: {
        no: "",
        carCode: "",
        grossWeight: "",
        tareWeight: "",
        netWeight: "",
        grossDateTime: "",
        garbageType: "",
        recycleType: "",
      },
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getWDMData();
    this.wdmPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/weighbridge-data-manage") {
        this.wdmPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      this.wdmPermissions = newVal;
    },
  },
  methods: {
    // 垃圾类型
    selectGarbageValue(value) {
      if (this.editVisible) {
        this.editForm.garbageType = value;
      } else {
        this.$set(this.query, "garbageType", value);
      }
    },
    // 可回收类型
    selectRecycleValue(value) {
      this.editForm.recycleType = value;
    },
    getWDMData() {
      this.$axios
        .get("/weight/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            garbageType: this.query.garbageType,
            no: this.query.no,
            carCode: this.query.carCode,
            grossDateTime1: this.value2
              ? this.value2[0].getTime().toString()
              : "",
            grossDateTime2: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
        })
        .then(({ data: res }) => {
          console.log("地磅数据", res);
          if (res.code !== 0) {
            return;
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;

          this.tableData.map((item, index) => {
            // var pageIndex = this.query.pageIndex;
            // item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            item.grossDateTime = this.dateFormat(item.grossDateTime);

            switch (item.garbageType) {
              case 1:
                item.garbageTypeName = "易腐垃圾";
                break;
              case 2:
                item.garbageTypeName = "可回收物";
                break;
              case 3:
                item.garbageTypeName = "其他垃圾";
                break;
              case 4:
                item.garbageTypeName = "有害垃圾";
                break;
              case 5:
                item.garbageTypeName = "大件垃圾";
                break;
              case 6:
                item.garbageTypeName = "装修垃圾";
                break;
            }

            switch (item.recycleType) {
              case 21:
                item.recycleTypeName = "金属";
                break;
              case 22:
                item.recycleTypeName = "塑料";
                break;
              case 23:
                item.recycleTypeName = "玻璃";
                break;
              case 24:
                item.recycleTypeName = "纺织";
                break;
              case 25:
                item.recycleTypeName = "纸类";
                break;
              case 26:
                item.recycleTypeName = "其他";
                break;
              case 27:
                item.recycleTypeName = "小家电";
                break;
            }
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      NProgress.start();
      this.$axios
        .get("/weight/export", {
          params: {
            garbageType: this.query.garbageType,
            no: this.query.no,
            carCode: this.query.carCode,
            grossDateTime1: this.value2
              ? this.value2[0].getTime().toString()
              : "",
            grossDateTime2: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
          responseType: "blob",
        })
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          NProgress.done();
        })
        .catch((error) => {
          this.$Notice.error({ title: "错误", desc: "网络连接错误" });
        });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getWDMData();
    },

    // 删除操作
    handleDelete(index, row) {
      console.log(index, row);
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.weightId;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      this.$axios
        .delete("/weight/delete", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getWDMData();
        })
        .catch(() => {});
    },

    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row);
      this.idx = index;
      this.id = row.weightId;
      this.editVisible = true;
      // 获取编辑数据
      this.getEdit(row.weightId);
    },
    getEdit(weightId) {
      this.$axios
        .get(`/weight/${weightId}`)
        .then(({ data: res }) => {
          console.log("编辑数据", res);
          if (res.code != 0) {
            return;
          }

          res.data.grossDateTime = this.dateFormat(res.data.grossDateTime);
          this.garbageTypeValue = "";
          this.recycleTypeValue = "";
          switch (res.data.garbageType) {
            case 1:
              this.garbageTypeValue = "易腐垃圾";

              break;
            case 2:
              this.garbageTypeValue = "可回收物";

              break;
            case 3:
              this.garbageTypeValue = "其他垃圾";

              break;
            case 4:
              this.garbageTypeValue = "有害垃圾";

              break;
            case 5:
              this.garbageTypeValue = "大件垃圾";

              break;
            case 6:
              this.garbageTypeValue = "装修垃圾";

              break;
          }

          switch (res.data.recycleType) {
            case 21:
              this.recycleTypeValue = "金属";

              break;
            case 22:
              this.recycleTypeValue = "塑料";

              break;
            case 23:
              this.recycleTypeValue = "玻璃";

              break;
            case 24:
              this.recycleTypeValue = "纺织";

              break;
            case 25:
              this.recycleTypeValue = "纸类";

              break;
            case 26:
              this.recycleTypeValue = "其他";

              break;
            case 27:
              this.recycleTypeValue = "小家电";

              break;
          }

          this.editForm = {
            no: res.data.no,
            carCode: res.data.carCode,
            grossWeight: res.data.grossWeight,
            tareWeight: res.data.tareWeight,
            netWeight: res.data.netWeight,
            grossDateTime: res.data.grossDateTime,
            garbageType: res.data.garbageType,
            recycleType: res.data.recycleType,
          };
        })
        .catch(() => {});
    },
    // 保存编辑
    saveEdit() {
      var bool = util.isEmpty({
        no: this.editForm.no,
        carCode: this.editForm.carCode,
        grossWeight: this.editForm.grossWeight,
        tareWeight: this.editForm.tareWeight,
        netWeight: this.editForm.netWeight,
        grossDateTime: this.editForm.grossDateTime,
        garbageType: this.editForm.garbageType,
      });
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      this.editForm.grossDateTime = new Date(
        this.editForm.grossDateTime
      ).getTime();

      this.$axios
        .put("/weight/update", {
          weightId: this.id,
          ...this.editForm,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return;
          }

          this.editVisible = false;
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          this.editForm = {
            no: "",
            carCode: "",
            grossWeight: "",
            tareWeight: "",
            netWeight: "",
            grossDateTime: "",
            garbageType: "",
            recycleType: "",
          };
          this.getWDMData();
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getWDMData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getWDMData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: auto;
}

.handle-input {
  width: auto;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
